package com.gopay.scheduler.job;

import com.gopay.common.domain.cert.CorpMsgSignCert;
import com.gopay.common.domain.cert.MsgGopaySignCert;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.service.cacert.CorpMsgSignCertService;
import com.gopay.scheduler.service.cacert.MsgGopaySignCertService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * description ：用于服务器证书私钥等敏感信息PCI加密数据更新的Job 将只执行一次 生产优化12期
 * edit by ：lx
 * datetime ：2017/11/23 16:26
 */
public class CorpAndGopayAPICertPCIJob extends GopayBaseJob {
    private Logger logger = LoggerFactory.getLogger(CorpAndGopayAPICertPCIJob.class);
    @Resource(name = "msgGopaySignCertService")
    private MsgGopaySignCertService msgGopaySignCertService;
    @Resource(name = "corpMsgSignCertService")
    private CorpMsgSignCertService corpMsgSignCertService;

    @Override
    protected void doJobInternal() {

        //执行Gopay服务器证书 数据的更新操作
        List<MsgGopaySignCert> gopayList = msgGopaySignCertService.getDataToEnc();
        if (!CollectionUtils.isEmpty(gopayList)) {
            logger.error("=============企业服务器证书待处理数据:{}条===============", gopayList.size());
            for (MsgGopaySignCert msgGopaySignCert : gopayList) {

                if (StringUtils.hasLength(msgGopaySignCert.getPrivateKey())) {
                    msgGopaySignCert.setPrivateKeyPCI(msgGopaySignCert.getPrivateKey());
                }

                if (StringUtils.hasLength(msgGopaySignCert.getCertSignBuf())) {
                    msgGopaySignCert.setCertSignBufPCI(msgGopaySignCert.getCertSignBuf());
                }

                if (StringUtils.hasLength(msgGopaySignCert.getPfxCert())) {
                    msgGopaySignCert.setPfxCertPCI(msgGopaySignCert.getPfxCert());
                }

                //msgGopaySignCertService.updateEncData(msgGopaySignCert);

            }
        }
        //执行Corp服务器证书 数据更新操作
        List<CorpMsgSignCert> corplist = corpMsgSignCertService.getDataToEnc();
        if (!CollectionUtils.isEmpty(corplist)) {
            logger.error("=============企业服务器证书待处理数据:{}条===============", corplist.size());
            for (CorpMsgSignCert corpMsgSignCert : corplist) {
                if (StringUtils.hasLength(corpMsgSignCert.getPrivateKey())) {
                    corpMsgSignCert.setPrivateKeyPCI(corpMsgSignCert.getPrivateKey());
                }

                if (StringUtils.hasLength(corpMsgSignCert.getCertSignBuf())) {
                    corpMsgSignCert.setCertSignBufPCI(corpMsgSignCert.getCertSignBuf());
                }

                if (StringUtils.hasLength(corpMsgSignCert.getPfxCert())) {
                    corpMsgSignCert.setPfxCertPCI(corpMsgSignCert.getPfxCert());
                }

               // corpMsgSignCertService.updateEncData(corpMsgSignCert);

            }
        }
    }
}
